#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import pathlib
import platform

if platform.system() == "Darwin":
    file_path = r"/Applications/VSCodium.app/Contents/Resources/app/extensions/markdown-basics/language-configuration.json"
    vscode_path = r"/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/markdown-basics/language-configuration.json"
elif platform.system() == "Linux":
    file_path = r"/usr/share/codium/resources/app/extensions/markdown-basics/language-configuration.json"
    vscode_path = r"/usr/share/code/resources/app/extensions/markdown-basics/language-configuration.json"
elif platform.system() == "Windows":
    file_path = rf"{pathlib.Path().home()}/scoop/apps/vscodium/current/resources/app/extensions/markdown-basics/language-configuration.json"
    vscode_path = rf"{pathlib.Path().home()}/scoop/apps/vscode/current/resources/app/extensions/markdown-basics/language-configuration.json"


def read_check_write(path):
    if pathlib.Path(path).is_file():
        with open(file=path, mode="r", encoding="utf-8") as f:
            js_dict = json.load(f)

        auto_close_pairs = js_dict["autoClosingPairs"]
        if {"open": "`", "close": "`"} in auto_close_pairs:
            pass
        else:
            auto_close_pairs.append({"open": "`", "close": "`"})
            with open(file=path, mode="w", encoding="utf-8") as f:
                json.dump(js_dict, f, indent=4, ensure_ascii=False)


if __name__ == "__main__":
    read_check_write(file_path)
    read_check_write(vscode_path)
